---
// Import the global.css file here so that it is included on
// all pages through the use of the <BaseHead /> component.
import "@unocss/reset/tailwind.css";
import "../styles/global.css";
interface Props {
  title: string;
  description: string;
  image?: string;
}

const canonicalURL = new URL(Astro.url.pathname, Astro.site);

const { title, description, image = "/blog-placeholder-1.jpg" } = Astro.props;
---

<!-- Global Metadata -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="icon" type="image/x-icon" href="/images/johnbakhmat.webp" />
<meta name="generator" content={Astro.generator} />

<link rel="sitemap" href="/sitemap-index.xml" />

<!-- Font preloads -->
<link
  rel="preload"
  href="/fonts/MartianMono-Regular.woff2"
  as="font"
  type="font/woff"
  crossorigin
/>
<link
  rel="preload"
  href="/fonts/hack-regular-subset.woff2"
  as="font"
  type="font/woff"
  crossorigin
/>

<!-- Canonical URL -->
<link rel="canonical" href={canonicalURL} />

<!-- Primary Meta Tags -->
<title>{title}</title>
<meta name="title" content={title} />
<meta name="description" content={description} />

<!-- Open Graph / Facebook -->
<meta property="og:type" content="website" />
<meta property="og:url" content={Astro.url} />
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:image" content={new URL(image, Astro.url)} />

<!-- Twitter -->
<meta property="twitter:card" content="summary_large_image" />
<meta property="twitter:url" content={Astro.url} />
<meta property="twitter:title" content={title} />
<meta property="twitter:description" content={description} />
<meta property="twitter:image" content={new URL(image, Astro.url)} />

<script defer>
  (() => {
    "use strict";

    const history = window.history;
    const location = window.location;
    const document = window.document;

    const shouldIgnore = () => {
      try {
        return (
          /^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(
            location.hostname,
          ) ||
          "file:" === location.protocol ||
          window._phantom ||
          window.__nightmare ||
          window.navigator.webdriver ||
          window.Cypress ||
          window.localStorage.plausible_ignore === "true"
        );
      } catch (t) {}

      return false;
    };

    function sendEvent(url, extra) {
      if (shouldIgnore()) return;

      const body = {
        n: url,
        u: location.href,
        d: "johnbakhmat.tech",
        r: document.referrer || null,
        p: extra?.props ?? undefined,
        m: extra?.meta ? JSON.stringify(extra.meta) : undefined,
      };

      fetch("/_internal/stats", {
        method: "POST",
        headers: { "Content-Type": "text/plain" },
        body: JSON.stringify(body),
      }).catch(function (t) {
        console.warn("Failed sending event", t);
      });
    }

    window.plausible = sendEvent;

    let lastPage = null;

    function onPageChange() {
      if (location.pathname === lastPage) return;

      sendEvent("pageview");
      lastPage = location.pathname;
    }

    if (history.pushState) {
      const originalPushState = history.pushState;
      history.pushState = function () {
        originalPushState.apply(this, arguments);
        onPageChange();
      };
      addEventListener("popstate", onPageChange);

      if (document.visibilityState === "prerender") {
        document.addEventListener("visibilitychange", () => {
          if (lastPage !== null || document.visibilityState === "visible")
            return;

          onPageChange();
        });
        return;
      }

      onPageChange();
    }
  })();
</script>

